#include "lin_master.h"

/* brief default LIN master initial configuration */
static const UART_LinConfig_t subCaseUartLinMasterCfgx =
{
    19200, 40000000, UART_LIN_MASTER, DISABLE
};

/*定义了各报文的信息*/
LIN_FRAME_INFO linframe[LIN_ID_MAX] = 
{
    {
    .uartLinHeader = {LIN_ID_BCM, 13, 1}, 
    .uartLinMasterResponse = {UART_LIN_CHECKSUM_ENHANCED, 8, {0, 0, 0, 0, 0, 0, 0, 0}},
    .msg_type = LIN_MSG_TYPE_TX,
    },
    {
    .uartLinHeader = {LIN_ID_E12, 13, 1}, 
    .uartLinMasterResponse = {UART_LIN_CHECKSUM_ENHANCED, 8, {0, 0, 0, 0, 0, 0, 0, 0}},
    .msg_type = LIN_MSG_TYPE_TX,
    },
};

/*
功能：数据场发送/接收完成的回调
参数：无
返回：无
备注：
*/
static void lin_response_finish_callback_master()
{
    uint32_t pid;
    pid = UART_LinGetId(LIN_MASTER_UART_ID);
    for (uint8_t i = 0; i < LIN_ID_MAX; i++)
    {
        if(pid == linframe[i].uartLinHeader.id)
        {
            if(linframe[i].msg_type == LIN_MSG_TYPE_TX)
            {
                
            }
            else
            {
                UART_LinReadResponse(LIN_MASTER_UART_ID, linframe[i].uartLinMasterResponse.len, &linframe[i].uartLinMasterResponse.data[0]);
            }
            break;
        }
    }
}

/*
功能：帧头发送完成的回调
参数：无
返回：无
备注：
*/
static void lin_header_finish_callback_master()
{
    uint32_t pid;
    pid = UART_LinGetId(LIN_MASTER_UART_ID);

    for (uint8_t i = 0; i < LIN_ID_MAX; i++)
    {
        if(pid == linframe[i].uartLinHeader.id)
        {
            if(linframe[i].msg_type == LIN_MSG_TYPE_TX)
            {
                UART_LinSendResponse(LIN_MASTER_UART_ID, &linframe[i].uartLinMasterResponse);
            }
            else
            {
                UART_LinStartReceiveResponse(LIN_MASTER_UART_ID, linframe[i].uartLinMasterResponse.checkType, linframe[i].uartLinMasterResponse.len);
            }
            break;
        }
    }
}

/*
功能：调度表执行，需放在定时器中断里，调度周期即为中断周期
参数：无
返回：无
备注：
*/
void lin_PeriodHandle(void)
{
    static uint8_t i = 0;
    
    /*10ms period sent Frame*/
    if( SET == UART_LinGetTransmissionStatus(LIN_MASTER_UART_ID))
    {    
        /*set Header receive status*/
        UART_LinStopTransmission(LIN_MASTER_UART_ID);
    }
    /* Master: send header */
    UART_LinSendHeader(LIN_MASTER_UART_ID, &linframe[i].uartLinHeader);   
    if(++i >= LIN_ID_MAX) i = 0;                
}

/*
功能：初始化LIN相关模块和IO，并注册回调
参数：无
返回：无
备注：
*/
void lin_master_Init(void)
{
    /* Set module clock */
    /* Select uart0 clock */
    CLK_ModuleSrc(CLK_UART0, CLK_SRC_OSC40M);
    /* Set function clock divider */
    CLK_SetClkDivider(CLK_UART0, CLK_DIV_2);
    /* Enable module */
    CLK_ModuleSrc(CLK_PORTA, CLK_SRC_OSC40M);
    CLK_ModuleSrc(CLK_PORTC, CLK_SRC_OSC40M);
    SYSCTRL_EnableModule(SYSCTRL_PORTA);
    SYSCTRL_EnableModule(SYSCTRL_PORTC);
    SYSCTRL_EnableModule(SYSCTRL_UART0);
    SYSCTRL_EnableModule(SYSCTRL_GPIO);
    /* Install callback function */
    UART_InstallCallBackFunc(LIN_MASTER_UART_ID, UART_INT_HEADER_DONE, lin_header_finish_callback_master);
    UART_InstallCallBackFunc(LIN_MASTER_UART_ID, UART_INT_RSP_DONE, lin_response_finish_callback_master);
    UART_IntMask(LIN_MASTER_UART_ID, UART_INT_HEADER_DONE, UNMASK);
    UART_IntMask(LIN_MASTER_UART_ID, UART_INT_RSP_DONE, UNMASK);
    /*When UART re-open after close, need to check and clear busy status*/
    while(ERR == UART_WaitBusyClear(LIN_MASTER_UART_ID, 7000))
    {
        (void)UART_ReceiveByte(LIN_MASTER_UART_ID);
    }
    /* Module init */
    UART_LinConfig(LIN_MASTER_UART_ID, &subCaseUartLinMasterCfgx);
    /* enable IRQ*/
    NVIC_EnableIRQ(UART0_IRQn);
    /* Todo: Set pinmux */
    PORT_PinmuxConfig(PORT_A, GPIO_12, PTA12_UART0_RX);
    PORT_PinmuxConfig(PORT_A, GPIO_13, PTA13_UART0_TX);
    PORT_PinmuxConfig(PORT_C, GPIO_6, PTC6_GPIO);
    GPIO_SetPinDir(PORT_C, GPIO_6, GPIO_OUTPUT);
    GPIO_SetPinOutput(PORT_C, GPIO_6);
}